<style lang="less">
    @import "../../../styles/table.less";
</style>
<template>
    <div class="search">
        <Row>
            <Col>
                <Card title="版本管理">
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" sortable="custom" @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table"></Table>
                    </Row>

                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>
        <versions-drawer ref="versionsDrawer"></versions-drawer>
    </div>
</template>

<script>
    import {versionsList,setForceUpdate} from "@/api/bus";

    import circleLoading from "../../my-components/circle-loading.vue";
    import versionsDrawer from "./versions-drawer";
    export default {
        name: "versions-manage",
        components: {
            circleLoading,
            versionsDrawer
        },
        data() {
            return {
                loading: false, // 表单加载状态
                operationLoading: false,
                companyId: "",
                drop: false,
                selectCount: 0, // 多选计数
                selectList: [], // 多选数据
                dropDownContent: "展开",
                dropDownIcon: "ios-arrow-down",
                department: [],
                searchForm: {
                    pageNumber: 1, // 当前页数
                    pageSize: 10, // 页面大小
                    sort: "createTime", // 默认排序字段
                    order: "desc", // 默认排序方式
                    planName: "",
                },
                selectDate: null, //选择日期绑定modal
                columns: [
                    {
                        type: "selection",
                        width: 60,
                        align: "center"
                    },
                    {
                        type: "index",
                        width: 60,
                        align: "center"
                    },
                    {
                        title: "版本名称",
                        key: "verName",
                        width: 180,
                        sortable: true
                    },
                    {
                        title: "版本号",
                        key: "verNo",
                        width: 180,
                        sortable: true,
                        align: "center"
                    },
                    {
                        title: "版本标题",
                        key: "verTitle",
                        width: 180,
                        align: "center"
                    },
                    /*{
                        title: "更新内容",
                        key: "verContent",
                        width: 180,
                        align: "center"
                    },*/
                    {
                        title: "强制更新",
                        key: "forceUpdate",
                        align: "center",
                        width: 180,
                        render: (h, params) => {
                            return h(
                                "i-switch",
                                {
                                    props: {
                                        value: params.row.forceUpdate,
                                        size: "large",
                                        "true-value": 1,
                                        "false-value": 2
                                    },
                                    on: {
                                        "on-change": v => {
                                            this.setForceUpdate(params.row, v);
                                        }
                                    }
                                },
                                [
                                    h("span", {slot: "open"}, "是"),
                                    h("span", {slot: "close"}, "否")
                                ]
                            );
                        }
                    },
                    {
                        title: "操作",
                        key: "action",
                        align: "center",
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            size: "small"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    "编辑"
                                )
                            ]);
                        }
                    }],
                data: [], // 表单数据
                total: 0 // 表单数据总数
            }
        },
        methods: {
            init() {
                this.getDataList();
            },
            getDataList() {
                // 多条件搜索用户列表
                this.loading = true;
                versionsList(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.status === true) {
                        this.data = res.data.rows;
                        this.total = res.data.total;
                    }
                });
            },
            setForceUpdate(row, v) {
                let params = {
                    id: row.id,
                    forceUpdate: v
                }
                setForceUpdate(params).then(res => {
                    if (res.status === true) {
                        this.$Message.success(res.info);
                    }
                });
                ;
            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getDataList();
            },
            handleReset() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.selectDate = null;
                this.searchForm.planName = "";
                // 重新加载数据
                this.getDataList();
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            selectDateRange(v) {
                if (v) {
                    this.searchForm.startDate = v[0];
                    this.searchForm.endDate = v[1];
                }
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getDataList();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getDataList();
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getDataList();
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            add() {
                let query = {type: 0}
                this.$refs.planDrawer.showDrawer(query);
            },
            edit(row) {
                let query = {type: 1, id: row.id};
                this.$refs.versionsDrawer.showDrawer(query);
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            showSelect(e) {
                this.exportData = e;
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            delAll() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择要删除的数据");
                    return;
                }
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
                    onOk: () => {
                        let ids = "";
                        this.selectList.forEach(function (e) {
                            ids += e.id + ",";
                        });
                        ids = ids.substring(0, ids.length - 1);
                        delStaff(ids).then(res => {
                            if (res.status === true) {
                                this.$Message.success("删除成功");
                                this.clearSelectAll();
                                this.getDataList();
                            }
                        });
                    }
                });
            },
        },
        mounted() {
            this.init();
        }
    }
</script>
